<template>
  <a-modal
    title="新增指标分类"
    :width="700"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleSubmit"
    @cancel="handleCancel"
  >
    <div style="height: 400px; overflow-y: auto">
      <a-spin :spinning="confirmLoading">
        <a-form :form="form">
          <a-form-item v-show="false"><a-input v-decorator="['id']" /></a-form-item>

          <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="父级菜单" has-feedback>
            <a-tree-select
              v-decorator="['pid', { rules: [{ required: true, message: '请选择上级指标分类！' }] }]"
              show-search
              style="width: 350px"
              :dropdown-style="{ maxHeight: '400px', overflow: 'auto' }"
              placeholder="请选择上级指标分类"
              :treeData="typeTreeData"
              treeNodeFilterProp = "title"
            >
            </a-tree-select>
          </a-form-item>

          <a-form-item label="指标名称" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
            <a-input
              placeholder="请输入指标名称"
              v-decorator="['name', { rules: [{ required: true, message: '请输入指标名称！' }] }]"
              style="width: 350px"
            />
          </a-form-item>
          <a-form-item label="指标类型" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
            <a-select
              v-decorator="['type', { rules: [{ required: true, message: '请选择指标类型！' }] }]"
              aria-selected="全部"
              style="width: 350px"
              placeholder="请选择指标类型"
            >
              <a-select-option value=" "> 默认 </a-select-option>
              <a-select-option value="point"> point </a-select-option>
              <a-select-option value="line"> line </a-select-option>
              <a-select-option value="cover"> cover </a-select-option>
              <a-select-option value="grid"> grid </a-select-option>
            </a-select>
          </a-form-item>
          <!-- <a-form-item label="编码" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input placeholder="请输入编码" v-decorator="['code', { rules: [{ required: true, message: '请输入！' }] }]" />
        </a-form-item> -->
          <!-- <a-form-item label="指标级别" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input-number
            placeholder="请输入指标级别"
            v-decorator="['level', { rules: [{ required: true, message: '请输入指标级别' }] }]"
            @change="onChangelevel"
            style="width: 350px"
          />
        </a-form-item>

        <a-form-item label="排序" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input-number
            placeholder="请输入排序"
            v-decorator="['orderList', { rules: [{ required: true, message: '请输入排序！' }] }]"
            @change="onChange"
            :min="0"
            style="width: 350px"
          />
        </a-form-item> -->
          <!-- <a-form-item label="是否启用" :labelCol="labelCol" :wrapperCol="wrapperCol">
            <a-switch
              v-decorator="['isEnable', { rules: [{ required: true, message: '请选择状态！' }] }]"
              checked-children="是"
              un-checked-children="否"
            />
          </a-form-item> -->
          <a-row>
            <a-col :span="13">
              <a-form-item label="是否为默认指标" :labelCol="halfLabelCol" :wrapperCol="halfWrapperCol">
                <a-switch
                  v-decorator="['isDefaultFT', { valuePropName: 'checked' }]"
                  checked-children="是"
                  un-checked-children="否"
                />
              </a-form-item>
            </a-col>
            <a-col :span="13">
              <a-form-item label="是否为常用指标" :labelCol="halfLabelCol" :wrapperCol="halfWrapperCol">
                <a-switch
                  v-decorator="['isCommonFT', { valuePropName: 'checked' }]"
                  checked-children="是"
                  un-checked-children="否"
                />
              </a-form-item>
            </a-col>
          </a-row>
          <!-- <a-form-item
          label="父id"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input placeholder="请输入父id" v-decorator="['pid', {rules: [{required: true, message: '请输入父id！'}]}]" />
        </a-form-item> -->
          <!-- <a-form-item
          label=""
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input placeholder="请输入" v-decorator="['pids', {rules: [{required: true, message: '请输入！'}]}]" />
        </a-form-item> -->
          <!-- <a-form-item
          label="是否父分组 Y/N"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input placeholder="请输入是否父分组 Y/N" v-decorator="['isParent', {rules: [{required: true, message: '请输入是否父分组 Y/N！'}]}]" />
        </a-form-item> -->
          <!-- <a-form-item
          label="是否默认指标 Y/N"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input placeholder="请输入是否默认指标 Y/N" v-decorator="['isDefault', {rules: [{required: true, message: '请输入是否默认指标 Y/N！'}]}]" />
        </a-form-item>
        <a-form-item
          label="是否启用 Y/N"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input placeholder="请输入是否启用 Y/N" v-decorator="['isEnable', {rules: [{required: true, message: '请输入是否启用 Y/N！'}]}]" />
        </a-form-item> -->
          <a-form-item label="指标颜色" :labelCol="labelCol" :wrapperCol="wrapperCol">
            <colorPicker v-model="indexColor" />
          </a-form-item>
          <a-form-item label="说明" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
            <a-textarea
              placeholder="请输入说明"
              :auto-size="{ minRows: 5, maxRows: 10 }"
              v-decorator="['description']"
            />
          </a-form-item>
          <!-- <a-form-item
          label=""
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input placeholder="请输入" v-decorator="['source', {rules: [{required: true, message: '请输入！'}]}]" />
        </a-form-item> -->
        </a-form>
      </a-spin>
    </div>
  </a-modal>
</template>

<script>
import { biIndexTypeAdd, biIndexTypeSelectTree } from '@/api/modular/main/biindextype/biIndexTypeManage'
import vcolorpicker from 'vcolorpicker'
export default {
  components: {
    vcolorpicker,
  },
  data() {
    return {
      labelCol: {
        xs: { span: 24 },
        sm: { span: 4 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 19 },
      },
      halfLabelCol: { xs: { span: 48 }, sm: { span: 10 } },
      halfWrapperCol: { xs: { span: 48 }, sm: { span: 10 } },
      visible: false,
      confirmLoading: false,
      form: this.$form.createForm(this),
      typeTreeData: [],
      record: {},
      indexColor:"#1890ff"
    }
  },
  created() {
    this.loadSelectTree()
  },
  methods: {
    loadSelectTree() {
      biIndexTypeSelectTree({}).then((res) => {
        if (res.success) {
          this.typeTreeData = [
            {
              id: '-1',
              parentId: '0',
              title: '一级指标',
              value: '0',
              pid: '0',
              children: res.data,
            },
          ]
        }
      })
    },
    // 初始化方法
    add() {
      this.loadSelectTree()
      this.visible = true
      setTimeout(() => {
        this.form.setFieldsValue({
            type: "grid",
            isCommonFT: false,
            isDefaultFT: false,
          })
      }, 300)
    },
    /**
     * 提交表单
     */
    handleSubmit() {
      const {
        form: { validateFields },
      } = this
      this.confirmLoading = true
      validateFields((errors, values) => {
        if (!errors) {
          for (const key in values) {
            if (typeof values[key] === 'object') {
              values[key] = JSON.stringify(values[key])
            }
          }
          console.info(values)
          values.color = this.indexColor
          biIndexTypeAdd(values)
            .then((res) => {
              if (res.success) {
                this.$message.success('新增成功')
                this.confirmLoading = false
                this.$emit('ok', values)
                this.handleCancel()
              } else {
                this.$message.error(res.message) // + res.message
              }
            })
            .finally((res) => {
              this.confirmLoading = false
            })
        } else {
          this.confirmLoading = false
        }
      })
    },
    handleCancel() {
      this.form.resetFields()
      this.visible = false
    },
    onChange(orderList) {
      console.log('changed', orderList)
    },
    onChangelevel(level) {
      console.log('changed', level)
    },
  },
}
</script>
